/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution209 {

    public static void main(String args[]) {
        Solution209 s = new Solution209();
        System.out.println(s.minSubArrayLen(7, new int[]{2, 3, 2, 2, 4, 3}));
    }

    public int minSubArrayLen(int s, int[] nums) {
        if (nums.length == 0) {
            return 0;
        }

        int l = 0;
        int r = 0;
        int sum = nums[0];
        int minv = Integer.MAX_VALUE;

        while (true) {
            if (minv == 1 || (r >= nums.length - 1 && sum < s)) {
                break;
            }
            while (sum < s && r + 1 < nums.length) {
                r++;
                sum += nums[r];
            }
            if (sum >= s) {
                minv = Math.min(minv, r - l + 1);
            }

            while (sum >= s && l <= r) {
                minv = Math.min(minv, r - l + 1);
                sum -= nums[l];
                l++;
            }
        }
        return minv == Integer.MAX_VALUE ? 0 : minv;

    }
}
